import {intervalDays,frontAndBackDay} from "@/utils/util"
//整合获取到的预测日历数据为适合展示的数据
export const formatData = originalData => {
	//需要返回的数据
	let result = [];
	//过滤空的数据
	let data = filterData(originalData);
	//第一条数据是周几
	let mark = new Date(data[0].forecastDate).getDay() == 0 ? 7 : new Date(data[0].forecastDate).getDay();
	for(let i = 1; i < mark; i++) {
		data.unshift({"faker":true});
	}
	let rows = Math.ceil(data.length / 7);
	for(let i = 0; i < rows; i++) {
		let week = data.splice(0, 7);
		result.push(week);
	}
	return result;
}
//过滤空的数据
const filterData = shuju => {
	let data = JSON.parse(JSON.stringify(shuju));
	for(let i=0;i<data.length;i++){
		if(data[i+1]){
			let interval = intervalDays(data[i].forecastDate,data[i+1].forecastDate);	
			if(interval>1){
				for(let j=1;j<interval;j++){
					let obj = {
							"forecastDate":frontAndBackDay(data[i].forecastDate,j),
							"faker":true
						};					
					data.splice(i+j,0,obj);					
				}
				i = i+interval-1;				
			}				
		}				
	}	
	return data;
}

//echarts参数配置
export const eOption = data => {
	let X = getX(data);//X轴的日期坐标
	let seat = getVal(data,'seat');//seat座位数
	let demand = getVal(data,'demand');//demand需求数
	let bookingNum = getVal(data,'bookingNum');//bookingNum预定数
	let checkinNum = getVal(data,'checkinNum');//checkinNum登机数
	let attendanceRate = getVal(data,'attendanceRate');//attendanceRate上座率
	let maxY = getMax(data) + Math.ceil(getMax(data)/5)//从获取数据获取左侧Y轴最大值
	let intervalY = Math.ceil(maxY/5);//从获取数据获取左侧Y轴间隔
	let option = {
		tooltip: {
			trigger: 'axis',
			axisPointer: {
				type: 'cross',
				crossStyle: {
					color: '#999'
				}
			}
		},
		grid: {
			left: '60px',
			right: '60px',
		},
		toolbox: {
			feature: {
				dataView: { show: true, readOnly: false },
				magicType: { show: true, type: ['line', 'bar'] },
				restore: { show: true },
				saveAsImage: { show: true }
			}
		},
		legend: {
			data: ['座位数', '需求', '登机', '上座率'],
			orient: 'horizontal',
			y: 'bottom',
		},
		xAxis: [{
			type: 'category',
			data: X,
			axisPointer: {
				type: 'shadow'
			}
		}],
		yAxis: [{
				type: 'value',
				name: '数量',
				min: 0,
				max: maxY,
				interval: intervalY,
				axisLabel: {
					formatter: '{value}'
				}
			},
			{
				type: 'value',
				name: '上座率',
				min: 0,
				max: 100,
				interval: 20,
				axisLabel: {
					formatter: '{value} %'
				}
			}
		],
		series: [{
				name: '座位数',
				type: 'bar',
				data: seat
			},
			{
				name: '需求',
				type: 'bar',
				data: demand
			},
			// {
			// 	name: '预定数',
			// 	type: 'bar',
			// 	data: bookingNum
			// },
			{
				name: '登机',
				type: 'bar',
				data: checkinNum
			},
			{
				name: '上座率',
				type: 'line',
				yAxisIndex: 1,
				data: attendanceRate
			}
		]
	};
	return option;
}
//获取X轴所有的日期
const getX = data => {
	let res = [];
	for(let i=0;i<data.length;i++){
		let Date = (data[i].forecastDate).substr(8,10);
		res.push(Date);
	}
	return res;
}
//获取各个类型的值
const getVal = (data,type) => {
	let res = [];
	for(let i=0;i<data.length;i++){
		let val = (data[i][type]);
		res.push(val);
	}
	return res;
}
//获取数量的最大值
const getMax = data => {
	let res = [];
	for(let i=0;i<data.length;i++){
		for(let j in data[i]){
			if(j=='seat'||j=='demand'||j=='checkinNum'){
				res.push(data[i][j])
			}
		}
	}	
	return Math.max.apply( Math, res );
}